package com.usermgr.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;


import com.opensymphony.xwork2.ActionSupport;
import com.usermgr.bean.OfUser;
import com.usermgr.biz.IUserManager;

import org.apache.struts2.ServletActionContext;
/**
 *@title      	标题：
 *@description 	描述：
 *@author 	  	作者 ：wuxionghao
 *@createTime 	创建时间：2011-12-29下午12:37:55
 *@version    	版本：v1.0
 */
public class UserMgrAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private OfUser user;
	
	private IUserManager userManager;
	
	//用户登录，验证用户是否合法
	@Override
	public String execute() throws IOException{
		String username = user.getOfName();	//取得用户名
		String password = user.getOfPassword();	//取得用户密码
		String result = "";
		OfUser user1 = new OfUser();
		user1.setOfName(username);
		user1.setOfPassword(password);
		if(userManager.findUser(user)){
			result = "success";
			ServletActionContext.getRequest().getSession().setAttribute("userName", user.getOfName());
			Long userId = userManager.getUserId(username, password);
			ServletActionContext.getRequest().getSession().setAttribute("userId", userId);
			List<OfUser> ofUser=userManager.findPermission(user);
			for(int i=0;i<ofUser.size();i++){
				OfUser user2=new OfUser();
				user2.setOfPermission(ofUser.get(i).getOfPermission());
				ServletActionContext.getRequest().getSession().setAttribute("userPermission", user2.getOfPermission());
			}
		}else{
			ServletActionContext.getResponse().getWriter().write( "用户名或密码输入有误！登入失败...");
			ServletActionContext.getRequest().setAttribute("failed", "用户名或密码输入有误！登入失败...");
			result = "failed";
		}
		return result;
	}
	//用户注册
	public String userRegister(){
		String username = user.getOfName();	//取得用户名
		String password = user.getOfPassword();	//取得用户密码
		String result = "";
		OfUser reginsterUser = new OfUser();
		reginsterUser.setOfName(username);
		reginsterUser.setOfPassword(password);
		reginsterUser.setOfTime(new Date());
		if(userManager.addUser(reginsterUser)){
			result = "success";
		}else{
			result = "failed";
		}
		return result;
	}
	/**
	 * 修改密码
	 * @author meishuangyan
	 * @return
	 */
	public String updateUserPassword(){
		String newPassword=ServletActionContext.getRequest().getParameter("ofPassword1");
		Long userId = Long.parseLong(ServletActionContext.getRequest().getSession().getAttribute("userId").toString());
		if(userManager.updateUserPassword(newPassword, userId)){
			return "success";
		}
		return "error";
	}
	
	/**
	 * 查看所有的用户
	 * @author meishuangyan
	 * @return
	 */
	public String getAllUser(){
		List<OfUser> list=userManager.getAllUser();
		if(list!=null&&list.size()>0){
			ServletActionContext.getRequest().setAttribute("userList", list);
			return "success";
		}
		return "error";
	}
	
	/**
	 * 删除用户（只有管理员有此权限）
	 * @author meishuangyan
	 * @return
	 */
	public String deleteUser(){
		String ofUserId=ServletActionContext.getRequest().getParameter("ofId");
		long userId=Long.parseLong(ofUserId);
		int reslut=-1;
		reslut = userManager.deleteUser(userId);
		if(reslut>0){
			return "success";
		}
		return "error";
	}
	/**
	 * 验证用户是否存在
	 * @author meishuangyan
	 * @return
	 */
	public void existUserName() throws IOException{
		ServletActionContext.getRequest();
		String userName=ServletActionContext.getRequest().getParameter("userName");
		String name=userName;
		if(userManager.chexkUser(name)){
			ServletActionContext.getResponse().getWriter().write("该用户已经存在！请改换用户名..");
		}else{
			ServletActionContext.getResponse().getWriter().write("可用");
		}
	}
	/**
	 * 清空session
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public String cleanSession(){
		ServletActionContext.getRequest().getSession().removeAttribute("userName");
		ServletActionContext.getRequest().getSession().removeAttribute("userId");
		ServletActionContext.getRequest().getSession().removeAttribute("userPermissions");
		return "success";
	}
	public OfUser getUser() {
		return user;
	}
	public void setUser(OfUser user) {
		this.user = user;
	}

	public IUserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}
}
